package z_mxsTest.testScrollableComposite;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class testScrollableComposite {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("带滚动条的SWT窗口");
        shell.setSize(500, 400);
        shell.setLayout(new FillLayout());

        // 创建ScrolledComposite作为容器
        ScrolledComposite scrolledComposite = new ScrolledComposite(shell, 
                SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        
        // 创建内容Composite
        Composite contentComposite = new Composite(scrolledComposite, SWT.NONE);
        contentComposite.setLayout(new GridLayout(1, false));
        
        // 添加大量控件以触发滚动条
        for (int i = 0; i < 30; i++) {
            Button button = new Button(contentComposite, SWT.PUSH);
            button.setText("按钮 " + (i + 1));
            button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        }
        
        // 设置ScrolledComposite的内容和大小
        scrolledComposite.setContent(contentComposite);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        
        // 必须设置minSize，否则不会显示滚动条
        contentComposite.pack();
        scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}